package com.example.zuul.filter;

import com.example.zuul.otherservice.Otherservice;
import com.example.zuul.return_data.Mall_Data;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

@Component
public class Mall_filter extends ZuulFilter {

    @Autowired
    Otherservice otherservice;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        //获取当前请求的上下文
        RequestContext requestContext = RequestContext.getCurrentContext();
        //获取当次被拦截的请求对象
        HttpServletRequest request = requestContext.getRequest();
        HttpServletResponse resp = requestContext.getResponse();
            //允许任何请求来源
            resp.addHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
            //允许跨域请求携带cookie
            resp.addHeader("Access-Control-Allow-Credentials", "true");
            // 跨域信息
            resp.addHeader("Access-Control-Allow-Headers", "*");
            resp.addHeader("Access-Control-Allow-Methods", "*");





        //如果发现请求是该路径，则表示为去认证中心进行登录和token的赋予，则不对该请求做任何操作
       // System.out.println(request.getRequestURI()+"=================");
        String regx = "^/user/g_[\\w]+";
       //商城菜单查询请求
        String memu = "/memurbac/reachbyuseridmemu";
        //按照用户id查询用户名
        String selectmydata = "/user/selectmydata";
        //按照用户id查询用户名
        String forgetphone = "/user/forgetphone";
        //按照菜单类型查询菜单
        String selectmydata1 = "/memurbac/reachbytypeidmemu";
        //商城菜单查询请求
        String goods = "/goods/g_cnxh";
//-------------------------------------------------欧阳华林
        String selectmydata2 = "/goods/ppp";

        String selectmydata3 = "/goods/shopss";

        String selectmydata4 = "/goods/fl";

        String selectmydata5 = "/goods/www";

        String selectmydata6 = "/goods/oooo";

        String selectmydata7 = "/goods/sp";

        String selectmydata8 = "/goods/sp1";

        String selectmydata9 = "/goods/px";

        String selectmydata10 = "/goods/di";

        String selectmydata11 = "/goods/gao";

        String selectmydata12 = "/goods/jiage";

        String selectmydata13 = "/goods/jiage1";

        String selectmydata14 = "/goods/indata";

        String selectmydata16 = "/goods/pppp";

        String selectmydata17 = "/goods/gwc";
//--------------------------------------------陈彬
        String cb = "/goods/selallgoodsdetails";
        String cb1 = "/goods/selordernum";
        String cb2 = "/goods/sssss";
        String cb3 = "/goods/updacom";
        String cb4 = "/goods/deletecom";
        String cb5 = "/goods/deletecomgg";
        String cb6 = "/goods/selallorder";
        String cb7 = "/goods/fahuo";
        String cb8 = "/goods/selorderby";
        String cb9 = "/goods/sel_userdianpu";
        String cb10 = "/goods/kaitongdianpu";
        String cb11 = "/goods/selgoodsleimu";
        String cb12 = "/goods/selgoodsattribute";
        String cb13 = "/goods/inseleibie";
        String cb14 = "/goods/inseleibie";
        String cb15 = "/goods/inseleibie";
        String cb16 = "/goods/updaleibie";
        String cb17 = "/goods/addattribute";
        String cb18 = "/goods/addcategoryconfig";
        String cb19 = "/goods/deleleibie";

        String cb20 = "/goods/selgoodsleimu";
        String cb21 = "/goods/selgoodsattribute";
        String cb22 = "/goods/addgoods";
        //----------------------------------------大斌


//----------------------------------------大斌

        String db = "/goods/selectqbpl";
        String db1 = "/goods/selectshopliul";
        String db2 = "/goods/update";
        String db3 = "/goods/selectidshangp";
        String db4 = "/goods/selectinset";
        String db5 = "/goods/mhcx";

//------------------------------------------支付宝支付
        String selectmydata15 = "/pay/pay";

//----------------------------------------- 熙熙嘻
        String ljx1 ="/goods/selectgoods";
        String ljx2 ="/goods/selectpicture";
        String ljx3 ="/goods/selectattribute";
        String ljx4 ="/goods/selectconfiguration_type";
        String ljx5 ="/goods/selectcolor";
        String ljx6 ="/goods/selecter";
        String ljx7 ="/goods/selectsan";
        String ljx8 ="/goods/selectsi";
        String ljx9 ="/goods/selectpirce";
        String ljx10 ="/goods/selectstock";


        if (Pattern.matches(regx,request.getRequestURI())
                ||Pattern.matches(memu,request.getRequestURI())
                ||Pattern.matches(selectmydata,request.getRequestURI())
                ||Pattern.matches(selectmydata1,request.getRequestURI())
                ||Pattern.matches(selectmydata2,request.getRequestURI())
                ||Pattern.matches(selectmydata3,request.getRequestURI())
                ||Pattern.matches(selectmydata4,request.getRequestURI())
                ||Pattern.matches(selectmydata5,request.getRequestURI())
                ||Pattern.matches(selectmydata6,request.getRequestURI())
                ||Pattern.matches(selectmydata7,request.getRequestURI())
                ||Pattern.matches(selectmydata8,request.getRequestURI())
                ||Pattern.matches(selectmydata9,request.getRequestURI())
                ||Pattern.matches(selectmydata10,request.getRequestURI())
                ||Pattern.matches(selectmydata11,request.getRequestURI())
                ||Pattern.matches(selectmydata12,request.getRequestURI())
                ||Pattern.matches(selectmydata13,request.getRequestURI())
                ||Pattern.matches(selectmydata14,request.getRequestURI())
                ||Pattern.matches(selectmydata15,request.getRequestURI())
                ||Pattern.matches(selectmydata16,request.getRequestURI())
                ||Pattern.matches(selectmydata17,request.getRequestURI())

                ||Pattern.matches(cb,request.getRequestURI())
                ||Pattern.matches(cb1,request.getRequestURI())
                ||Pattern.matches(cb2,request.getRequestURI())
                ||Pattern.matches(cb3,request.getRequestURI())
                ||Pattern.matches(cb4,request.getRequestURI())
                ||Pattern.matches(cb5,request.getRequestURI())
                ||Pattern.matches(cb6,request.getRequestURI())
                ||Pattern.matches(cb7,request.getRequestURI())
                ||Pattern.matches(cb8,request.getRequestURI())
                ||Pattern.matches(cb9,request.getRequestURI())
                ||Pattern.matches(cb10,request.getRequestURI())
                ||Pattern.matches(cb11,request.getRequestURI())
                ||Pattern.matches(cb12,request.getRequestURI())
                ||Pattern.matches(cb13,request.getRequestURI())
                ||Pattern.matches(cb14,request.getRequestURI())
                ||Pattern.matches(cb15,request.getRequestURI())
                ||Pattern.matches(cb16,request.getRequestURI())
                ||Pattern.matches(cb17,request.getRequestURI())
                ||Pattern.matches(cb18,request.getRequestURI())
                ||Pattern.matches(cb19,request.getRequestURI())
                ||Pattern.matches(cb20,request.getRequestURI())
                ||Pattern.matches(cb21,request.getRequestURI())
                ||Pattern.matches(cb22,request.getRequestURI())

                ||Pattern.matches(goods,request.getRequestURI())


                ||Pattern.matches(db,request.getRequestURI())
                ||Pattern.matches(db1,request.getRequestURI())
                ||Pattern.matches(db2,request.getRequestURI())
                ||Pattern.matches(db3,request.getRequestURI())
                ||Pattern.matches(db4,request.getRequestURI())
                ||Pattern.matches(db5,request.getRequestURI())


                ||Pattern.matches(ljx1,request.getRequestURI())
                ||Pattern.matches(ljx2,request.getRequestURI())
                ||Pattern.matches(ljx3,request.getRequestURI())
                ||Pattern.matches(ljx4,request.getRequestURI())
                ||Pattern.matches(ljx5,request.getRequestURI())
                ||Pattern.matches(ljx6,request.getRequestURI())
                ||Pattern.matches(ljx7,request.getRequestURI())
                ||Pattern.matches(ljx8,request.getRequestURI())
                ||Pattern.matches(ljx9,request.getRequestURI())
                ||Pattern.matches(ljx10,request.getRequestURI())
                ||Pattern.matches(forgetphone,request.getRequestURI())
        ){

        } else {



           //获取所有Cookie数组
            Cookie[] cookies=request.getCookies();
          //是否携带token的标记
            boolean path=false;
            //保存token
            String token="";
            if(cookies!=null){
                for (Cookie cookie : cookies) {
                    if(cookie.getName().equals("token")){
                        path=true;
                        token=cookie.getValue();

                    }

                }
            }

            //如果该请求头存在token
            if (path){

                //鉴定 token
               Mall_Data res=otherservice.checkAcs(token);
                /*System.out.println(res.toString());*/
               //验证成功
               if((int)res.get("code")==200){
                   if(!((String)res.get("data")).equals("鉴权成功")){
                       Cookie cookie = new Cookie("token",(String)res.get("data"));
                       cookie.setMaxAge(36000);
                       cookie.setDomain("localhost");
                       cookie.setPath("/");
                       resp.addCookie(cookie);
                   }
                return null;
               }else {

                   requestContext.setSendZuulResponse(false);
                   requestContext.setResponseStatusCode((int)res.get("code"));
                   requestContext.setResponseBody((String)res.get("error"));

               }
            } else {

                requestContext.setSendZuulResponse(false);
                requestContext.setResponseStatusCode(520);
                requestContext.setResponseBody("The current session is not logged in ,please login !");

            }

        }
        return null;
    }


}